//
//  UIButton.swift
//  新浪微博
//
//  Created by 李国伟 on 15/12/6.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit
extension UIButton
{
    /**
     遍历构造函数
     
     :param: imageNamed     图片
     :param: backImageNamed 背景图片
     
     :returns: 返回按钮
     */
    convenience init(imageNamed:String,backImageNamed:String) {
        self.init()
        setImage(UIImage(named: imageNamed), forState: UIControlState.Normal)
        setImage(UIImage(named: imageNamed+"_highlighted"), forState: UIControlState.Highlighted)
        if backImageNamed != "" {
        setBackgroundImage(UIImage(named: backImageNamed), forState: UIControlState.Normal)
        setBackgroundImage(UIImage(named: backImageNamed+"_highlighted"), forState: UIControlState.Highlighted)
        }
        sizeToFit()
    }
    
    /**
     设置带背景颜色的按钮
     
     :param: imageName 图片名称
     :param: title     按钮标题
     :param: color     文字颜色
     
     :returns: 按钮
     */
    convenience init(backimageName:String,title:String,color:UIColor){
        self.init()
        setTitleColor(color, forState: .Normal)
        setBackgroundImage(UIImage(named: backimageName), forState: .Normal)
        setTitle(title, forState: .Normal)
        sizeToFit()
    }
    
    /**
     设置带背景颜色的按钮
     
     :param: imageName 图片名称
     :param: title     按钮标题
     :param: color     文字颜色
     
     :returns: 按钮
     */
    convenience init(imageName:String?,title:String,fontsize: CGFloat,color:UIColor,backColor: UIColor? = nil){
        self.init()
        setTitleColor(color, forState: .Normal)
        if let image = imageName {
           setImage(UIImage(named: image), forState: .Normal)
        }
        backgroundColor = backColor
        setTitle(title, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontsize)
       
        sizeToFit()
    }
}